package com.muze.core.app.upload.httppost;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.commons.compress.utils.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;

public class HTTPPost {

	public static void main(String[] args) {
		
		String strjson ="{'machineId':'HST821255555',"
				+ "'macAddr':'00-06-55-55-0A-2B',"
				+ "'member': {'name':'你好 ','phone':'15812397368','idcard':'440883199354284257','age':'22','sex':'男','address':'北京海淀区','measureTime':'2015-04-0914:32:02','birthday':'1993-09-28','barcode':'','userIcon':''},"
				+ "'height':{'height':'180','weight':'70','BMI':'22','idealWeight':'74'},"
				+ "'fat':{'fatRate':'16','fat':'26.2','exceptFat':'49.8','waterRate':'36.4','water':'26.8','minerals':'1.2','protein':'7.6','fic':'17.8','foc':'9','muscle':'34.4', 'fatAdjust':'-0.3','weightAdjust':'0.1','muscleAdjust':'0.4','basicMetabolism':'1140','viscera':'7','result':''},"
				+ "'bloodPressure':{'highPressure':'96','lowPressure':'57','pulse':'65','result':''},"
				+ "'bo':{'bo':'99','result':'','startTime':'','endTime':'','listBo':'','listBoPulse':''},"
				+ "'ecg':{'ecg':'88','ecgData':'','result':'','nGain':''},"
				+ "'temperature':{'temperature':'37','result':''},"
				+ "'whr':{'waistline':'22','hipline':'88','whr':'25','result':''},"
				+ "'bloodSugar':{'bloodSugar':'3.8','us':'0.54','chol':'2.77','bloodsugarType':'','result':''}}";
        System.out.println(post("http://localhost:8080/muzeupload/UploadDataController/loadData",strjson));
	}
	
	public static String post(String url,String jsonString){
		HttpClient client = new DefaultHttpClient();
		HttpParams params = client.getParams();
		HttpConnectionParams.setConnectionTimeout(params, 0);
		HttpConnectionParams.setSoTimeout(params, 0);
		params.setParameter(HTTP.CONTENT_TYPE, "UTF-8");
		HttpPost post = new HttpPost(url);
		//post.addHeader("accept-encoding", "gzip,deflate");
		try {
			post.setEntity(new StringEntity(jsonString, "utf-8"));
			HttpResponse response = client.execute(post);
			InputStream input = response.getEntity().getContent();
			String responseString = new String(IOUtils.toByteArray(input), "UTF-8");
			System.out.println(responseString);
			return responseString;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
